<?php
namespace app\forum\controller;
use app\forum\controller\UserController;
/**
 * 用户设置
 */

class UserSettingController extends UserController {

	/**
     * 资料修改
     */
    public function index(){
    	if(!IS_POST){
	    	//MEDIA信息
	    	$this->crumb[] = array('url'=>url('index'),'name'=>'用户资料');
	        $this->siteDisplay();
	    }else{
	    	$_POST['user_id'] = USER_ID;
            if(target('User')->saveInfo()){
                $this->success('资料修改成功！',url('index'));
            }else{
                $msg = target('User')->getError();
                if(empty($msg)){
                    $this->error('资料修改失败');
                }else{
                    $this->error($msg);
                }
            }
	    }
    }

    /**
     * 头像修改
     */
    public function avatar(){
    	if(!IS_POST){
	    	//MEDIA信息
	    	$this->crumb[] = array('url'=>url('avatar'),'name'=>'用户头像');
	        $this->siteDisplay();
	    }else{
	    	$file = target('base/Upload');
	    	//处理上传
	    	$config = array();
	    	$config['DIR_NAME'] = 'attachment/' . date('Y-m-d');
	    	$config['UPLOAD_EXTS'] = 'jpg,gif,png,jpeg';
	    	$config['UPLOAD_SIZE'] = 1;
	    	$config['WATER_STATUS'] = false;
	    	$config['THUMB_STATUS'] = true;
	    	$config['THUMB_WIDTH'] = 128;
	    	$config['THUMB_HEIGHT'] = 128;
	    	$config['THUMB_TYPE'] = 3;
	    	$config['IMAGE_TEXT'] = 'jpg';
	    	$info = $file->upload($config);
	    	if($info){
	    		//移动头像
	    		$file = $info['url'];
	    		$file = str_replace(ROOT_URL, '', $file);
	    		$file = realpath(ROOT_PATH . $file);
	    		$path = pathinfo($file);
	    		$content = file_get_contents($file);
	    		$avatarFile = 'upload/avatar/'.USER_ID.'.'.$path['extension'];
	    		if(!file_put_contents(ROOT_PATH.$avatarFile, $content)){
	    			$this->error('头像修改失败');
	    		}
	    		$this->success('头像修改成功！',url('avatar'));
	        }else{
	            $msg = $file->getError();
                if(empty($msg)){
                    $this->error('头像修改失败');
                }else{
                    $this->error($msg);
                }
	        }
	    }
    }
    
}